package com.mljr.example.adapter.tab1;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ctakit.sdk.app.utils.ImgConfig;
import com.ctakit.sdk.view.ViewInjector;
import com.ctakit.sdk.view.annotation.ViewInject;
import com.mljr.example.R;
import com.mljr.example.adapter.BaseListAdapter;
import com.mljr.example.bean.tab1.Merchant;
import com.mljr.example.util.AppUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by andyhome on 16/4/20.
 */
public class MerchantAdapter extends BaseListAdapter<Merchant> {

    private boolean isNoData;
    private int mHeight;
    public static final int ONE_SCREEN_COUNT = 7; // 一屏能显示的个数，这个根据屏幕高度和各自的需求定
    public static final int ONE_REQUEST_COUNT = 10; // 一次请求的个数

    public MerchantAdapter(Context context) {
        super(context);
    }

    public MerchantAdapter(Context context, List<Merchant> list) {
        super(context, list);
    }

    // 设置数据
    public void setData(List<Merchant> list) {
        clearAll();
        addALL(list);


        if (list.size() == 1 && list.get(0).getId()==0) {
            // 暂无数据布局
            isNoData = true;
            mHeight = list.get(0).getHeight();
        } else {
            // 添加空数据
            if (list.size() < ONE_SCREEN_COUNT) {
                addALL(createEmptyList(ONE_SCREEN_COUNT - list.size()));
            }
        }
        notifyDataSetChanged();
    }

    // 创建不满一屏的空数据
    public List<Merchant> createEmptyList(int size) {
        List<Merchant> emptyList = new ArrayList<>();
        if (size <= 0) return emptyList;
        for (int i=0; i<size; i++) {
            emptyList.add(new Merchant());
        }
        return emptyList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 暂无数据
        if (isNoData) {
            convertView = mInflater.inflate(R.layout.item_no_data_layout, null);
            AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mHeight);
            RelativeLayout rootView = (RelativeLayout) convertView.findViewById(R.id.rl_root_view);
            rootView.setLayoutParams(params);
            return convertView;
        }

        // 正常数据
        final ViewHolder holder;
        if (convertView != null && convertView instanceof LinearLayout) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = mInflater.inflate(R.layout.tab1_item_merchant, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }

        Merchant entity = getItem(position);

        holder.llRootView.setVisibility(View.VISIBLE);


        holder.tvName.setText(entity.getMerchantName());
        holder.tvAddress.setText(entity.getAddress());
        holder.tvDistance.setText(entity.getDistance());
        holder.averagePrice.setText("人均￥"+entity.getAveragePrice()+"");
        holder.orderNumber.setText("已售"+entity.getOrderNumber()+"");
        holder.coolcardDiscountInfo.setText(entity.getCoolcardDiscountInfo()+"");

        ImgConfig.loadImg(AppUtils.getContext(), entity.getStorePictureUrl(), holder.ivImage);

        return convertView;
    }

    static class ViewHolder {
        @ViewInject(R.id.ll_root_view)
        LinearLayout llRootView;
        @ViewInject(R.id.iv_image)
        ImageView ivImage;
        @ViewInject(R.id.tv_name)
        TextView tvName;
        @ViewInject(R.id.tv_address)
        TextView tvAddress;
        @ViewInject(R.id.tv_distance)
        TextView tvDistance;
        @ViewInject(R.id.averagePrice)
        TextView averagePrice;
        @ViewInject(R.id.orderNumber)
        TextView orderNumber;
        @ViewInject(R.id.coolcardDiscountInfo)
        TextView coolcardDiscountInfo;


        ViewHolder(View view) {
            ViewInjector.inject(this, view);
        }
    }
}
